package cn.itsource.service;

import cn.hutool.http.HttpUtil;
import cn.itsource.config.WechatProperties;
import cn.itsource.constants.Constants;
import cn.itsource.pojo.dto.RegisterDto;
import com.alibaba.fastjson.JSONObject;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class WechatService {

    private WechatProperties wechatProperties;
     public RegisterDto  getWechatOpenId(String code){
        //拼接请求地址
        String sendUrl = String.format(Constants.CODE_2_SESSION_URL, wechatProperties.getAppid(), wechatProperties.getAppSecret(),code);
        log.info(sendUrl);//打印日志只需要打印核心
        //发送请求 用的huto工具包
        String result = HttpUtil.get(sendUrl);//返回的是json字符串
        //江json字符串转为对象
        return JSONObject.parseObject(result, RegisterDto.class);
    }
}
